﻿#region File Header
//-----------------------------------------------------------------------------
// Bricks' Bane
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SibLib.GameComponents;
using SibLib.GameState;
using SibLib.Text;

namespace BricksBane.Screens
{
    sealed class ResultScreen : GameScreen
    {
        private PlaySession m_PlaySession;

        private ContentManager m_Content;
        private SpriteBatch m_SpriteBatch;
        private SpriteFont m_SpriteFont;
        private Texture2D m_BackgroundTexture;

        public ResultScreen(PlaySession session) :
            base()
        {
            m_PlaySession = session;

            TransitionOffTime = TimeSpan.FromSeconds(1.0f);
            TransitionOnTime = TimeSpan.FromSeconds(0.5f);
        }

        public override void LoadContent()
        {
            base.LoadContent();

            CheckAndInsertHighscore();

            m_Content = new ContentManager(ScreenManager.Game.Services, "Content");

            m_SpriteBatch = new SpriteBatch(ScreenManager.GraphicsDevice);
            m_SpriteFont = m_Content.Load<SpriteFont>("SpriteFontMenu");

            m_BackgroundTexture = m_Content.Load<Texture2D>("Textures/Menu/background");
        }

        public override void UnloadContent()
        {
            base.UnloadContent();

            m_Content.Unload();
        }

        public override void HandleInput(GameTime gameTime, InputState input)
        {
            PlayerIndex index;
            if (input.IsMenuSelect(ControllingPlayer, out index))
            {
                LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(), new MainMenuScreen());
            }
        }

        private void CheckAndInsertHighscore()
        {
            // TODO: check if has new high score.
            // TODO: limit maximum score count
            HighscoreComponent.Instance.SetNewScore(ControllingPlayer.Value, (long)m_PlaySession.m_Score, m_PlaySession.m_Mode.ToString());
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            m_SpriteBatch.Begin();

            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);

            string text;
            Color bgColor;

            if (m_PlaySession.m_Mode == E_GameModes.Campaign)
            {
                if (m_PlaySession.m_LivesRemaining > 0)
                {
                    bgColor = Color.Lime;
                    text = "Campaign Complete!";
                }
                else
                {
                    bgColor = Color.Red;
                    text = "Game Over!";
                }
            }
            else if (m_PlaySession.m_Mode == E_GameModes.TimeAttack)
            {
                if (m_PlaySession.m_SecondsRemaining > 0.0f)
                {
                    bgColor = Color.Lime;
                    text = "Level Complete!";
                }
                else
                {
                    bgColor = Color.Red;
                    text = "Time Over!";
                }
            }
            else if (m_PlaySession.m_Mode == E_GameModes.ScoreAttack)
            {
                if (m_PlaySession.m_LivesRemaining > 0)
                {
                    bgColor = Color.Lime;
                    text = "Level Complete!";
                }
                else
                {
                    bgColor = Color.Red;
                    text = "Game Over!";
                }
            }
            else //if (m_PlaySession.m_Mode == E_GameModes.Marathon)
            {
                bgColor = Color.White;
                text = "Game Over!";
            }

            m_SpriteBatch.Draw(m_BackgroundTexture, fullscreen, bgColor);

            Vector2 position;
            position = new Vector2(viewport.Width / 2.0f - (m_SpriteFont.MeasureString(text).X / 2.0f), 140.0f);
            m_SpriteBatch.DrawString(m_SpriteFont, text, position + Vector2.One, Color.Black);
            m_SpriteBatch.DrawString(m_SpriteFont, text, position, Color.White);

            text = "Your score:";
            position = new Vector2(viewport.Width / 2.0f - (m_SpriteFont.MeasureString(text).X / 2.0f), 300.0f);
            m_SpriteBatch.DrawString(m_SpriteFont, text, position, Color.White);

            text = m_PlaySession.m_Score.ToString();    // TODO: garbage, may not care here.
            position = new Vector2(viewport.Width / 2.0f - (m_SpriteFont.MeasureString(text).X / 2.0f), 370.0f);
            m_SpriteBatch.DrawString(m_SpriteFont, text, position, Color.White);

            if (m_PlaySession.m_Mode == E_GameModes.TimeAttack)
            {
                text = "Your time:";
                position = new Vector2(viewport.Width / 2.0f - (m_SpriteFont.MeasureString(text).X / 2.0f), 450.0f);
                m_SpriteBatch.DrawString(m_SpriteFont, text, position, Color.White);

                StringBuilder builder = new StringBuilder(8);    // TODO: garbage, may not care here.
                StringTools.TimeToStringBuilder(builder, (int)m_PlaySession.m_SecondsRemaining);
                position = new Vector2(viewport.Width / 2.0f - (m_SpriteFont.MeasureString(builder).X / 2.0f), 520.0f);
                m_SpriteBatch.DrawString(m_SpriteFont, builder, position, Color.White);
            }

            m_SpriteBatch.End();

            // Fade it out to black transition.
            if (TransitionPosition > 0)
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
        }
    }
}
